package com.uade.webservices.logisticaMonitoreo;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;

import javax.jms.Destination;


import javax.jms.MessageProducer;

import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.apache.log4j.Logger;


import com.uade.model.Configuracion;
import com.uade.model.Modelo;

public class LogJMS extends LogSenderStrategy {
	private Logger log = Logger.getLogger(LogSenderStrategy.class);

	public LogJMS(Configuracion conf) {
		this.configuracion = conf;
	}

	@Override
	public void enviarLog(String xml) {
		try {
			log.info("Se envia el xml: " + xml);
			final Properties env = new Properties();
			String connectionFactoryString = "";

			if (null != configuracion.getTipoSistema()
					&& configuracion.getTipoSistema().equals(
							Modelo.EJB.toString())) {
				env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory");
				// Perform the JNDI lookups
				connectionFactoryString = System.getProperty(
						"connection.factory", "jms/RemoteConnectionFactory");
			} else {
				env.put(Context.INITIAL_CONTEXT_FACTORY,
						"org.jboss.naming.remote.client.InitialContextFactory");
				connectionFactoryString = System.getProperty(
						"connection.factory", "jms/RemoteConnectionFactory");
			}
			env.put(Context.PROVIDER_URL,
					System.getProperty(Context.PROVIDER_URL, "remote://"
							+ this.configuracion.getIp() + ":4447"));
			env.put(Context.SECURITY_PRINCIPAL,
					System.getProperty("username", "log"));
			env.put(Context.SECURITY_CREDENTIALS,
					System.getProperty("password", "log1234"));
			Context context = new InitialContext(env);

			ConnectionFactory connectionFactory = (ConnectionFactory) context
					.lookup(connectionFactoryString);

			String destinationString = System.getProperty("destination",
					"jms/queue/log");
			Destination destination = (Destination) context
					.lookup(destinationString);

			// Create the JMS connection, session, producer, and consumer
			Connection connection = connectionFactory.createConnection(
					System.getProperty("username", "log"),
					System.getProperty("password", "log1234"));
			Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);
			connection.start();

			MessageProducer producer = session.createProducer(destination);

			// crear un mensaje de tipo text y setearle el contenido
			TextMessage message = session.createTextMessage();
			message.setText(xml);

			// enviar el mensaje
			producer.send(message);

			connection.close();

		} catch (Exception ex) {

			log.info("Se produjo un error en el envio de Log JMS a monitoreo"
					+ ex.getMessage());

		}

	}

//	private static Logger log = Logger.getLogger(LogSenderStrategy.class);
//
//	private Connection connection = null;
//
//	// Set up all the default values
//	private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
//	private static final String DEFAULT_DESTINATION = "jms/queue/log";
//	private static final String DEFAULT_USERNAME = "log";
//	private static final String DEFAULT_PASSWORD = "log1234";
//	private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
//	private static String PROVIDER_URL = "remote://localhost:4447";
//
//	public LogJMS(Configuracion conf) {
//		this.configuracion = conf;
//	}
//
//	@Override
//	public void enviarLog(String xml) throws LogisticaMonitoreoException {
//		LogJMS.enviarLogS(xml,this.configuracion.getIp());
//	}
//	
//	
//	public static void enviarLogS(String xml,String ip) throws LogisticaMonitoreoException {
//
//		ConnectionFactory connectionFactory = null;
//		Connection connection = null;
//		Session session = null;
//		MessageProducer producer = null;
//		Destination destination = null;
//		Context context = null;
//
//		// Set up the context for the JNDI lookup
//		final Properties env = new Properties();
//		env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
//		env.put(Context.PROVIDER_URL,
//				System.getProperty(Context.PROVIDER_URL, setearIp(ip)));
//		env.put(Context.SECURITY_PRINCIPAL,
//				System.getProperty("username", DEFAULT_USERNAME));
//		env.put(Context.SECURITY_CREDENTIALS,
//				System.getProperty("password", DEFAULT_PASSWORD));
//
//		try {
//
//			context = new InitialContext(env);
//
//			// Perform the JNDI lookups
//			String connectionFactoryString = System.getProperty(
//					"connection.factory", DEFAULT_CONNECTION_FACTORY);
//			connectionFactory = (ConnectionFactory) context
//					.lookup(connectionFactoryString);
//
//			String destinationString = System.getProperty("destination",
//					DEFAULT_DESTINATION);
//			destination = (Destination) context.lookup(destinationString);
//
//			// Create the JMS connection, session, producer, and consumer
//			connection = connectionFactory.createConnection(
//					System.getProperty("username", DEFAULT_USERNAME),
//					System.getProperty("password", DEFAULT_PASSWORD));
//			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//			producer = session.createProducer(destination);
//			connection.start();
//			// crear un mensaje de tipo text y setearle el contenido
//			TextMessage message;
//			message = session.createTextMessage();
//			message.setText(xml);
//			// enviar el mensaje
//			producer.send(message);
//			connection.close();
//		} catch (JMSException e) {
//			log.error("Portal Web: Se ha producido un error al tratar mandar un mensaje a Logistica y Monitoreo.");
//		} catch (NamingException e) {
//			log.error("Portal Web: Se ha producido un error al tratar de conectarse a la cola del Logistica y Monitoreo.");
//		}
//
//	}
//
//	public static String setearIp(String ip) {
//		return  "remote://" + ip + ":4447";
//	}

}
